package com.guan.mysolutionstudy;

import org.junit.Test;

/**
 * @ClassName MergeListNode
 * @Author GuanCary
 * @Date 2020/5/14 17:02
 * @Version 1.0
 * 剑指offer:输入两个单调递增的链表，输出两个链表合成后的链表，当然我们需要合成后的链表满足单调不减规则。
 * 思路:合并两个链表,并且单调不递减,那么可以直接改变链表引用,将next指向偏大的节点值
 **/
public class MergeListNode {
    public ListNode solution(ListNode listNode1, ListNode listNode2) {
        if (listNode1 == null) {
            return listNode2;
        }
        if (listNode2 == null) {
            return listNode1;
        }
        if (listNode1.value <= listNode2.value) {
            listNode1.next = solution(listNode1.next, listNode2);
            return listNode1;
        } else {
            listNode2.next = solution(listNode1, listNode2.next);
            return listNode2;
        }
    }

    @Test
    public void test() {
        ListNode solution = solution(new ListNode(1, new ListNode(3, new ListNode(7))),
                new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
        while (solution != null) {
            System.out.println(solution.value);
            solution = solution.next;
        }
    }
}